package com.system.user.controller;

import com.system.common.util.ImageUtil;
import com.system.common.util.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/Upload")
@CrossOrigin
public class UploadController {

    @PostMapping("/toUpload")
    public Result upload(@RequestParam("file") MultipartFile file){
        try {
            InputStream is = file.getInputStream();
            String fileName = file.getOriginalFilename();
            String newFileName = ImageUtil.upload(is,fileName);
            return Result.ok("图片上传成功",newFileName);
        } catch (IOException e) {
            throw new RuntimeException("图片上传失败");
        }
    }
}
